package com.herench.bs.transfer;

import org.apache.cxf.endpoint.Client;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import java.util.HashMap;
import java.util.Map;

/**
 * 患者提交入院申请
 *
 * @author liuxiaohui
 * @date 2019/3/26
 */
public class DoAppointInHosTransfer extends AbstractTransfer {
    public DoAppointInHosTransfer(String method, Client client, String messageMethod) {
        super(method, client, messageMethod);
    }

    /**
     * 发送：微信前端JSON数据转换为HIS用的XML数据（微信前端==》HIS）
     * @param params
     * @return
     * @throws Exception
     */
    @Override
    public String toInvokeMessage(Map<String, Object> params) throws Exception {
        // 患者ID
        String patientId = (String) params.get("patientId");
        // 入院次数
        String visitId = (String) params.get("visitId");
        // 患者姓名
        String patientName = (String) params.get("patientName");
        // 性别
        String sex = (String) params.get("sex");
        // 出生日期
        String birthDate = (String) params.get("birthDate");
        // 身份证号
        String idNo = (String) params.get("idNo");
        // 邮政编码
        String zipCode = (String) params.get("zipCode");
        // 联系电话
        String phoneNumber = (String) params.get("phoneNumber");
        // 入院方式
        String patientClass = (String) params.get("patientClass");
        // 入院病情
        String patAdmCondition = (String) params.get("patAdmCondition");
        // 入院目的
        String admissionCause = (String) params.get("admissionCause");
        // 押金金额
        String prepayment = (String) params.get("prepayment");
        // 入院科室编码
        String deptAddmissionTo = (String) params.get("deptAddmissionTo");
        // 门诊诊断
        String clinicDiagnosis = (String) params.get("clinicDiagnosis");
        // 门诊号
        String clinicId = (String) params.get("clinicId");
        // 预约入院个性需求ID
        String appInHosRequireId = (String) params.get("appInHosRequireId");
        // 预约入院个性需求选项序
        String appInHosRequireOptionSeq = (String) params.get("appInHosRequireOptionSeq");


        // 交易ID：his的方法名
        Document document = getTplDoc(getMethod());

        // 将各个入参数设值（入参==》XML）
        Element dataRow = document.getRootElement().element("CommitData").addElement("Data").addElement("DataRow");
        //Element dataRowRequire = document.getRootElement().element("CommitData").addElement("Require").addElement("DataRow");

        // 患者ID
        dataRow.addAttribute("PATIENT_ID", patientId);
        // 入院次数
        dataRow.addAttribute("VISIT_ID", visitId);
        // 患者姓名
        dataRow.addAttribute("PATIENT_NAME", patientName);
        // 性别
        dataRow.addAttribute("SEX", sex);
        // 出生日期
        dataRow.addAttribute("BIRTH_DATE", birthDate);
        // 身份证号
        dataRow.addAttribute("ID_NO", idNo);
        // 邮政编码
        dataRow.addAttribute("ZIP_CODE", zipCode);
        // 联系电话
        dataRow.addAttribute("PHONE_NUMBER", phoneNumber);
        // 入院方式
        dataRow.addAttribute("PATIENT_CLASS", patientClass);
        // 入院病情
        dataRow.addAttribute("PAT_ADM_CONDITION", patAdmCondition);
        // 入院目的
        dataRow.addAttribute("ADMISSION_CAUSE", admissionCause);
        // 押金金额
        dataRow.addAttribute("PREPAYMENT", prepayment);
        // 入院科室编码
        dataRow.addAttribute("DEPT_ADDMISSION_TO", deptAddmissionTo);
        // 门诊诊断
        dataRow.addAttribute("CLINIC_DIAGNOSIS", clinicDiagnosis);
        // 门诊号
        dataRow.addAttribute("CLINIC_ID", clinicId);

        // 预约入院个性需求ID
        //dataRowRequire.addAttribute("APP_IN_HOS_REQUIRE_ID", appInHosRequireId);
        // 预约入院个性需求选项序
        //dataRowRequire.addAttribute("APP_IN_HOS_REQUIRE_OPTION_SEQ", appInHosRequireOptionSeq);


        return document.asXML();
    }

    /**
     * 接收：HIS返回的XML数据并转换为JSON（HIS==》微信前端）
     * @param resMsg
     * @return
     * @throws Exception
     */
    @Override
    public Map<String, Object> toResultMessage(String resMsg) throws Exception {
        Map<String, Object> resMap = new HashMap<>(16);
        Document document = DocumentHelper.parseText(resMsg);

        Element resultInfo = document.getRootElement().element("Result").element("Info");
        String executeStatus = resultInfo.attributeValue("EXECUTE_STATUS");
        String executeMessage = resultInfo.attributeValue("EXECUTE_MESSAGE");

        if (Dialect.getSuccessfulState().equals(executeStatus)) {
            // 获取各个参数的值（出参的树状结构：父==》子）
            Element dataRow = document.getRootElement().element("ReturnData").element("Data").element("DataRow");
            if (dataRow == null) {
                resMap.put("code", "1403");
                resMap.put("message", "调用成功，但是获取的数据为 0 件！");
            } else {
                DoAppointInHosInnerMessage innerMessage = new DoAppointInHosInnerMessage(dataRow);
                // 入院申请ID
                resMap.put("hosAppId", innerMessage.getHosAppId());

                // 返回Code
                resMap.put("code", executeStatus);
                // 返回信息
                resMap.put("message", "成功！");
            }
        } else {
            resMap.put("code", executeStatus);
            resMap.put("message", executeMessage);
        }
        return resMap;
    }

    public class DoAppointInHosInnerMessage implements InnerMessage{
        /**
         * 入院申请ID
         */
        private String hosAppId;

        public DoAppointInHosInnerMessage(Element element) {
            // 入院申请ID
            this.hosAppId = element.attributeValue("HOS_APP_ID");
        }

        /**
         * 入院申请ID
         */
        public String getHosAppId() {
            return hosAppId;
        }
    }
}
